2 // AMXMLConsoleController.m
5 // Created by Andreas Monitzer on 2007-06-06.
6 // Copyright 2007 Andreas Monitzer. All rights reserved.
9 #import "AMXMLConsoleController.h"
10 #include <libpurple/jabber.h>
11 #import <AIUtilities/AIAutoScrollView.h>
13 #define XML_PREFIX @"<?xml version='1.0' encoding='UTF-8' ?>\n"
15 @interface AMXMLConsoleController (PRIVATE)
16 - (void)appendToLog:(NSAttributedString *)astr;
17 - (PurpleConnection *)gc;
21 xmlnode_received_cb(PurpleConnection *gc, xmlnode **packet, gpointer this)
23 AMXMLConsoleController *self = (AMXMLConsoleController *)this;
25 if (!this || [self gc] != gc)
28 char *str = xmlnode_to_formatted_str(*packet, NULL);
29 NSString *sstr = [NSString stringWithUTF8String:str];
31 if ([sstr hasPrefix:XML_PREFIX])
32 sstr = [sstr substringFromIndex:[XML_PREFIX length]];
34 NSAttributedString *astr = [[NSAttributedString alloc] initWithString:sstr
36 [self appendToLog:astr];
43 xmlnode_sent_cb(PurpleConnection *gc, char **packet, gpointer this)
45 AMXMLConsoleController *self = (AMXMLConsoleController *)this;
48 if (!this || [self gc] != gc)
51 node = ((*packet && strlen(*packet) && ((*packet)[0] == '<')) ?
52 xmlnode_from_str(*packet, -1) :
58 char *str = xmlnode_to_formatted_str(node, NULL);
59 NSString *sstr = [NSString stringWithUTF8String:str];
61 if ([sstr hasPrefix:XML_PREFIX])
62 sstr = [sstr substringFromIndex:[XML_PREFIX length]];
64 NSAttributedString *astr = [[NSAttributedString alloc] initWithString:sstr
65 attributes:[NSDictionary dictionaryWithObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName]];
66 [self appendToLog:astr];
73 @implementation AMXMLConsoleController
76 purple_signals_disconnect_by_handle(self);
81 - (IBAction)sendXML:(id)sender {
82 NSData *rawXMLData = [[xmlInjectView string] dataUsingEncoding:NSUTF8StringEncoding];
83 jabber_prpl_send_raw(gc, [rawXMLData bytes], [rawXMLData length]);
85 // remove from text field
86 [xmlInjectView setString:@""];
89 - (IBAction)clearLog:(id)sender {
90 [xmlLogView setString:@""];
93 - (IBAction)showWindow:(id)sender {
94 if (!xmlConsoleWindow) {
95 //Load the window if it's not already loaded
96 [NSBundle loadNibNamed:@"AMPurpleJabberXMLConsole" owner:self];
97 if (!xmlConsoleWindow) AILog(@"Unable to load AMPurpleJabberXMLConsole!");
100 //Connect to the signals for updating the window
101 PurplePlugin *jabber = purple_find_prpl("prpl-jabber");
102 if (!jabber) AILog(@"Unable to locate jabber prpl");
104 purple_signal_connect(jabber, "jabber-receiving-xmlnode", self,
105 PURPLE_CALLBACK(xmlnode_received_cb), self);
106 purple_signal_connect(jabber, "jabber-sending-text", self,
107 PURPLE_CALLBACK(xmlnode_sent_cb), self);
110 [xmlConsoleWindow makeKeyAndOrderFront:sender];
111 [(AIAutoScrollView *)[xmlLogView enclosingScrollView] setAutoScrollToBottom:YES];
114 - (void)windowWillClose:(NSNotification *)notification
116 xmlConsoleWindow = nil;
118 //We don't need to watch the signals with the window closed
119 purple_signals_disconnect_by_handle(self);
124 [xmlConsoleWindow close];
128 - (void)appendToLog:(NSAttributedString*)astr {
129 [[xmlLogView textStorage] appendAttributedString:astr];
132 - (PurpleConnection*)gc {
136 - (void)setPurpleConnection:(PurpleConnection *)inGc